/*
 * hopper_ev2000_interface.cpp
 *
 *  Created on: 25.12.2012
 *      Author: aska
 */

#include "hopper_ev2000_interface.h"
hopper_ev2000_lib::hopper_ev2000_lib(void)
{
	return;
}
int hopper_ev2000_lib::hopper_ev2000_dll_load()
{
	int ret_val = 0;

	h_hopper_ev2000_dll = LoadLibrary("../../Hopper_EV2000_Boost_DLL/Cross GCC Boost Debug/Hopper_EV2000_Boost_DLL.dll");
	//h_hopper_ev2000_dll = LoadLibrary("../../Hopper_EV2000_Boost_DLL/Release/Hopper_EV2000_Boost_DLL.dll");
	//h_hopper_ev2000_dll = LoadLibrary("../../Hopper_EV2000_Boost_DLL/Hopper_EV2000_Boost_DLL.dll");
	if (!(h_hopper_ev2000_dll > (void*)HINSTANCE_ERROR))
	{
		h_hopper_ev2000_dll = LoadLibrary("Hopper_EV2000_Boost_DLL.dll");
		if (!(h_hopper_ev2000_dll > (void*)HINSTANCE_ERROR))
		{
			printf("unable LoadLibrary(""Hopper_EV2000_Boost_DLL.dll"")");
		}
		else
		{
			cout << ". loaded" << endl;
		}
	}
	else
		cout << ".. loaded" << endl;
	int load_func_err = 0;
	int func_counter = 0;
	if(h_hopper_ev2000_dll > (void*)HINSTANCE_ERROR)
	{
		func_counter++;
		if (! (hopper_init = (t_hopper_init)GetProcAddress(h_hopper_ev2000_dll, "hopper_init")))
		{
			printf("Error locating hopper_init function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		if (! (hopper_init_prepare_mode_d2xx = (t_hopper_init_prepare_mode_d2xx)GetProcAddress(h_hopper_ev2000_dll, "hopper_init_prepare_mode_d2xx")))
		{
			printf("Error locating hopper_init_prepare_mode_d2xx function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		if (! (hopper_init_prepare_mode_d2xx_portNo = (t_hopper_init_prepare_mode_d2xx_portNo)GetProcAddress(h_hopper_ev2000_dll, "hopper_init_prepare_mode_d2xx_portNo")))
		{
			printf("Error locating hopper_init_prepare_mode_d2xx_portNo function \n");
			load_func_err |= 0x01 << func_counter;
		}
		func_counter++;
		if (! (hopper_close = (t_hopper_close)GetProcAddress(h_hopper_ev2000_dll, "hopper_close")))
		{
			printf("Error locating hopper_close function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		if (! (hopper_set_mode = (t_hopper_set_mode)GetProcAddress(h_hopper_ev2000_dll, "hopper_set_mode")))
		{
			printf("Error locating hopper_set_mode function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		if (! (hopper_port_open = (t_hopper_port_open)GetProcAddress(h_hopper_ev2000_dll, "hopper_port_open")))
		{
			printf("Error locating hopper_port_open function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		if (! (hopper_d2xx_open = (t_hopper_d2xx_open)GetProcAddress(h_hopper_ev2000_dll, "hopper_d2xx_open")))
		{
			printf("Error locating hopper_d2xx_open function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		if (! (hopper_send_simple_poll = (t_hopper_send_simple_poll)GetProcAddress(h_hopper_ev2000_dll, "hopper_send_simple_poll")))
		{
			printf("Error locating hopper_send_simple_poll function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		if (! (hopper_address_poll = (t_hopper_address_poll)GetProcAddress(h_hopper_ev2000_dll, "hopper_address_poll")))
		{
			printf("Error locating hopper_address_poll function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		if (! (hopper_prepare = (t_hopper_prepare)GetProcAddress(h_hopper_ev2000_dll, "hopper_prepare")))
		{
			printf("Error locating hopper_prepare function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		if (! (hopper_test_hopper = (t_hopper_test_hopper)GetProcAddress(h_hopper_ev2000_dll, "hopper_test_hopper")))
		{
			printf("Error locating hopper_test_hopper function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		if (! (hopper_request_status = (t_hopper_request_status)GetProcAddress(h_hopper_ev2000_dll, "hopper_request_status")))
		{
			printf("Error locating hopper_request_status function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		if (! (hopper_put_coins_ack = (t_hopper_put_coins_ack)GetProcAddress(h_hopper_ev2000_dll, "hopper_put_coins_ack")))
		{
			printf("Error locating hopper_put_coins_ack function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}

		//FreeLibrary(h_ser_port_dll);
	}
	else
	{
		printf(" dll not loaded\n");
		load_func_err = 0xFFFF;
	}
	return ret_val;
}
int hopper_ev2000_lib::dll_free(void)
{
	if(h_hopper_ev2000_dll > (void*)HINSTANCE_ERROR) FreeLibrary(h_hopper_ev2000_dll);
	return 0;
}

hopper_ev2000_lib::~hopper_ev2000_lib(void)
{
	return;
}
